import 'package:image_picker/image_picker.dart';

export 'package:image_picker/image_picker.dart';

/**
 * @Description: image_picker拍照  video_compress视频压缩 缩略图
 * @Author: 下雨了
 * @CreateDate: 2021-12-08 11:01
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-12-08 11:01
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

///
//定义一个top-level（全局）变量，
final imagePicker = ImagePickerUtils();

class ImagePickerUtils {
  late ImagePicker _picker;

  //私有构造函数
  ImagePickerUtils._internal() {
    _picker = ImagePicker();
  }

  //保存单例
  static final ImagePickerUtils _imagePickerUtils = ImagePickerUtils._internal();

  //工厂构造函数
  factory ImagePickerUtils() {
    return _imagePickerUtils;
  }

  // 打开相机 拍摄照片
  Future<XFile?> openCameraImage() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.camera, imageQuality: 100);
    return image;
  }

  // 打开相机 拍摄视频
  Future<XFile?> openCameraVideo() async {
    final XFile? video = await _picker.pickVideo(
      source: ImageSource.camera,
      preferredCameraDevice: CameraDevice.rear,
      maxDuration: const Duration(seconds: 60),
    );
    return video;
  }

  Future<List<XFile>> pickMultiImage({
    double? maxWidth,
    double? maxHeight,
    int? imageQuality,
    bool requestFullMetadata = true,
  }) async {
    final List<XFile> image = await _picker.pickMultiImage(
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      imageQuality: imageQuality,
      requestFullMetadata: requestFullMetadata,
    );
    return image;
  }

  Future<XFile?> pickImage({
    required ImageSource source,
    double? maxWidth,
    double? maxHeight,
    int? imageQuality,
  }) async {
    XFile? image = await ImagePicker().pickImage(
      source: source,
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      imageQuality: imageQuality,
    );
    return image;
  }

  Future<XFile?> pickVideo({
    required ImageSource source,
    CameraDevice preferredCameraDevice = CameraDevice.rear,
    Duration? maxDuration,
  }) async {
    XFile? video = await ImagePicker().pickVideo(
      source: source,
      preferredCameraDevice: preferredCameraDevice,
      maxDuration: maxDuration,
    );
    return video;
  }
}
